import * as taokeApi from '@/api/taoke'
var common = {};
common.goUrl = (info, _this) => {
	var that = _this;
	if (info.url_type == '1') {
		if (info.url == '') return false;
		uni.navigateTo({
			url: '/pages/webView/webView?url=' + encodeURIComponent(info.url)
		})
	} else if (info.url_type == '2') {
		if (info.url == '') return false;
		uni.navigateTo({
			url: info.url
		})
	} else if (info.url_type == '3') {
		// #ifdef MP-WEIXIN
		uni.showLoading()
		uni.navigateToMiniProgram({
			appId: info.appid,
			path: info.app_path,
			success(res) {
				uni.hideLoading()
			},
			fail(err) {
				uni.hideLoading()
			}
		})
		// #endif
	} else if (info.url_type == '4') {
		taokeApi.generateLink({
			'actId': info.act_id
		}).then((res) => {
			if (res.code == 1) {
				uni.hideLoading()
				// #ifdef H5
				window.location.href = (res.data.h5 == "" || res.data.h5 == null) ? res.data.longH5 : res
					.data.h5
				// #endif

				// #ifdef MP-WEIXIN
				uni.openEmbeddedMiniProgram({
					appId: res.data.weAppInfo.appId,
					path: res.data.weAppInfo.pagePath,
					allowFullScreen: true,
					success(res) {},
					fail(err) {}
				})
				// #endif
			} else {
				return  uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				
			}
		}).catch((e) => {
			return uni.showToast({
				title: "接口请求错误",
				icon: 'none'
			})
		})
	}

}
/**
 * 判断是否为空对象
 * @param {*} object 源对象
 */
common.isEmptyObject = (object) => {
	return Object.keys(object).length === 0
}


/**
 * 判断是否为空
 * @param {*} object 源对象
 */
common.isEmpty = (value) => {
	if (common.isArray(value)) {
		return value.length === 0
	}
	if (common.isObject(value)) {
		return common.isEmptyObject(value)
	}
	return !value
}

common.urlEncode = (obj = {}) => {
	const result = []
	for (const key in obj) {
		const item = obj[key]
		if (!item) {
			continue
		}
		if (common.isArray(item)) {
			item.forEach(val => {
				result.push(key + '=' + val)
			})
		} else {
			result.push(key + '=' + item)
		}
	}
	return result.join('&')
}

common.isArray = (array) => {
	return Object.prototype.toString.call(array) === '[object Array]'
}

common.isObject = (object) => {
	return Object.prototype.toString.call(object) === '[object Object]'
}


/**
 * 判断对象中是否存在某个属性
 * @param {*} object 源对象
 */
common.hasOwnProperty = (object, key) => {
  return common.isObject(object) && object[key] !== undefined
}


export default common